<?php
/**
 * Created by PhpStorm.
 * Author: D H
 * Date: 2017/6/16
 * Time: 10:34
 */

namespace app\api\logic;
use think\Db;
use tool\Common;

class Cat extends  Base{
        //得到顶级分类
        public function getTopCatList(){

           // return  $this->getUser();
        $result = Db::view('cat',['id' => 'catID','catName'])->where('parentID',0)->select();
        return Common::rm(1,'操作成功',['topCatList' => $result]);
    }
        //根据顶级分类ID得到下属所有分类
        public function getCatListByTopCatID(){
         //测试数据
           /* $arr = ' {
              "catID": 1
            }';
            $data = json_decode($arr,true);*/
           $data = $this->data;
            //查询出所有分类
            $list = Db::view('cat',['id' => 'catID','catName','parentID'])
                ->where('parentID',$data['catID'])
                ->select();
            $new_list = [];
            foreach ($list as $k => $val){
                $new_list[$k]['mediumCatName'] = $val['catName'];
                $_list = Db::view('cat',['id' => 'catID','catName'])
                    ->where('parentID',$val['catID'])
                    ->select();
                 $new_list[$k]['mediumCatVal'] = $_list;
            }
            return Common::rm(1,'操作成功',['catList' => $new_list]);

        }
        //根据分类ID得到品牌列表
        public function getBrandListByCatID(){
            //测试数据
          /* $arr ='{
                 "catID": 5
            }';
            $data = json_decode($arr,true);*/
            $data = $this->data;
            $list = Db::view('cat_brand',['name' => 'brandName'])
                  ->view('cat_middle','brandID','brandID = cat_brand.id')
                  ->where('cat_middle.catID',$data['catID'])
                  ->select();
            return Common::rm(1,'操作成功',['brandList' => $list]);
        }
}

